#include "OSAL.h"
#include "matrix.h"
#include "hal_board_cfg.h"


uint8 MatrixTaskId;
extern byte KeyboardTaskId;

uint8 keyStore[6] = {0};
uint8 size = 0;
uint8 keyMap48[48] = { 4, 5, 6, 7, 8, 9,10,11,12,13,14,15, \
                      16,17,18,19,20,21,22,23,24,25,26,27, \
                      28,29,30,31,32,33,34,35,36,37,38,39, \
                      28,29,30,31,32,33,34,35,36,37,38,39};
                      //40,41,42,43,44,45,46,47,48,49,50,51}; //0x33

void Matrix_Task_Init( byte task_id ) {
  
  MatrixTaskId = task_id;
  
  osal_set_event(MatrixTaskId, MATRIX_SCAN_EVENT);
  osal_set_event(MatrixTaskId, MATRIX_KEY_NOTUFY_EVENT);
}

uint16 Matrix_ProcessEvent( byte task_id, uint16 events ) 
{

  if ( events & MATRIX_SCAN_EVENT )
  {
    P0 = 0xFF;
    uint8 keyIdx = 0;
    if(P1 != 0x03) {
      uint8 saveP1 = P1;
      P0_0 = 0;  //0x07P12 0x0BP13  0x13P14  0x23P15  0x43P16 0x03P17
      if(saveP1 != P1) 
      {
        switch(saveP1)    
	{
          case 0x07: keyIdx = 1; break;
          case 0x0B: keyIdx = 2; break;
          case 0x13: keyIdx = 3; break;
          case 0x23: keyIdx = 4; break;
          case 0x43: keyIdx = 5; break;
          case 0x83: keyIdx = 6; break;
          default: keyIdx = 0; break;
	}
      }
      if(keyIdx>0) goto out;
      P0_1 = 0;
      if(saveP1 != P1) 
      {
        switch(saveP1)    
	{
          case 0x07: keyIdx = 7; break;
          case 0x0B: keyIdx = 8; break;
          case 0x13: keyIdx = 9; break;
          case 0x23: keyIdx = 10; break;
          case 0x43: keyIdx = 11; break;
          case 0x83: keyIdx = 12; break;
          default: keyIdx = 0; break;
	}
      }
      if(keyIdx>0) goto out;
      P0_2 = 0;
      if(saveP1 != P1) 
      {
        switch(saveP1)    
	{
          case 0x07: keyIdx = 13; break;
          case 0x0B: keyIdx = 14; break;
          case 0x13: keyIdx = 15; break;
          case 0x23: keyIdx = 16; break;
          case 0x43: keyIdx = 17; break;
          case 0x83: keyIdx = 18; break;
          default: keyIdx = 0; break;
	}
      }
      if(keyIdx>0) goto out;
      P0_3 = 0;
      if(saveP1 != P1) 
      {
        switch(saveP1)    
	{
          case 0x07: keyIdx = 19; break;
          case 0x0B: keyIdx = 20; break;
          case 0x13: keyIdx = 21; break;
          case 0x23: keyIdx = 22; break;
          case 0x43: keyIdx = 23; break;
          case 0x83: keyIdx = 24; break;
          default: keyIdx = 0; break;
	}
      }
      if(keyIdx>0) goto out;
      P0_4 = 0;
      if(saveP1 != P1) 
      {
        switch(saveP1)    
	{
          case 0x07: keyIdx = 25; break;
          case 0x0B: keyIdx = 26; break;
          case 0x13: keyIdx = 27; break;
          case 0x23: keyIdx = 28; break;
          case 0x43: keyIdx = 29; break;
          case 0x83: keyIdx = 30; break;
          default: keyIdx = 0; break;
	}
      }
      if(keyIdx>0) goto out;
      P0_5 = 0;
      if(saveP1 != P1) 
      {
        switch(saveP1)    
	{
          case 0x07: keyIdx = 31; break;
          case 0x0B: keyIdx = 32; break;
          case 0x13: keyIdx = 33; break;
          case 0x23: keyIdx = 34; break;
          case 0x43: keyIdx = 35; break;
          case 0x83: keyIdx = 36; break;
          default: keyIdx = 0; break;
	}
      }
      if(keyIdx>0) goto out;
      P0_6 = 0;
      if(saveP1 != P1) 
      {
        switch(saveP1)    
	{
          case 0x07: keyIdx = 37; break;
          case 0x0B: keyIdx = 38; break;
          case 0x13: keyIdx = 39; break;
          case 0x23: keyIdx = 40; break;
          case 0x43: keyIdx = 41; break;
          case 0x83: keyIdx = 42; break;
          default: keyIdx = 0; break;
	}
      }
      if(keyIdx>0) goto out;
      P0_7 = 0;
      if(saveP1 != P1) 
      {
        switch(saveP1)    
	{
          case 0x07: keyIdx = 43; break;
          case 0x0B: keyIdx = 44; break;
          case 0x13: keyIdx = 45; break;
          case 0x23: keyIdx = 46; break;
          case 0x43: keyIdx = 47; break;
          case 0x83: keyIdx = 48; break;
          default: keyIdx = 0; break;
	}
      }
      P0 = 0xFF;
out:  if(keyIdx > 0) {
        if(size<6) {
          keyStore[size++] = keyMap48[keyIdx-1];
        } else if(size == 6) {
          return events ^= MATRIX_SCAN_EVENT;
        }
        keyIdx = 0;
      }
    }
    osal_start_timerEx(MatrixTaskId, MATRIX_SCAN_EVENT, 5);
    events ^= MATRIX_SCAN_EVENT;
  }
  if ( events & MATRIX_KEY_NOTUFY_EVENT )
  {
    if(size > 0) {
      if(size == 6) {
        osal_start_timerEx(MatrixTaskId, MATRIX_SCAN_EVENT, 5);
      }
      keyValues_t *msgPtr = (keyValues_t *)osal_msg_allocate( sizeof(keyValues_t) );
      msgPtr->data = osal_msg_allocate(size * sizeof(uint8));
      if ( msgPtr )
      {
        osal_memcpy( msgPtr->data, keyStore, size );
        osal_memset(keyStore, 0x00, 6);
        msgPtr->hdr.status = size;
        msgPtr->hdr.event = MATRIX_KEY_PRESSED_MSG;
        osal_msg_send( KeyboardTaskId, (uint8 *)msgPtr );
      }
      size = 0;
    }
    osal_start_timerEx(MatrixTaskId, MATRIX_KEY_NOTUFY_EVENT, 8);
    events ^= MATRIX_KEY_NOTUFY_EVENT;
  }
  return events;
}
